package com.whu;

public class Simulation {
    // 宽度
    int width;
    // 长度
    int height;
    // 面板
    int[][] board;

    // 常量
    public static int DEAD = 0;
    public static int ALIVE = 1;

    public Simulation(int width, int height) {
        this.width = width;
        this.height = height;
        this.board = new int[width][height];
    }

    public static Simulation copy(Simulation simulation) {
        Simulation copy = new Simulation(simulation.width, simulation.height);
        for (int j = 0; j < simulation.height; j++) {
            for (int i = 0; i < simulation.width; i++) {
                copy.setState(i, j, simulation.getState(i,j));
            }
        }
        return copy;
    }

    // 初始化
    public void printBoard(){
        System.out.println("---");
        for (int j = 0; j < height; j++) {
            String line = "|";
            for (int i = 0; i < width; i++) {
                if(this.board[i][j] == 0){
                    line += ".";
                }else{
                    line += "*";
                }
            }
            line += "|";
            System.out.println(line);
        }
        System.out.println("---");
    }


    public void setAlive(int i, int j){
        setState(i, j, ALIVE);
    }

    public void setDead(int i, int j){
        setState(i, j, DEAD);
    }

    public void setState(int i, int j, int state){
        // 超出边界即为dead
        if(i < 0 || i >= width || j < 0 || j >= height){
            return;
        }
        this.board[i][j] = state;
    }



    public void step(){

        // 创建一个新的board面板，防止更新当前的影响遍历
        int[][] newBoard = new int[width][height];

        for (int j = 0; j < height; j++) {
            for (int i = 0; i < width; i++) {
                int aliveNeighbours = countAliveNeighbours(i, j);
                // 根据当前的，更新newBoard
                if(this.board[i][j] == 1){
                    if(aliveNeighbours < 2) {// 周围alve少于2个
                        newBoard[i][j] = DEAD;
                    }else if(aliveNeighbours == 2 || aliveNeighbours == 3){
                        newBoard[i][j] = ALIVE;
                    }else if(aliveNeighbours > 3){ // 周围多于三个，人口剩余
                        newBoard[i][j] = DEAD;
                    }
                }else {
                    if(aliveNeighbours == 3){
                        newBoard[i][j] = ALIVE;
                    }
                }
            }
        }
        this.board = newBoard;
    }

    public  int countAliveNeighbours(int i, int j){
        int count = 0;

        count += getState(i - 1,j - 1);
        count += getState(i, j - 1);
        count += getState(i + 1, j - 1);

        count += getState(i - 1, j);
        count += getState(i + 1, j);

        count += getState(i - 1, j + 1);
        count += getState(i, j + 1);
        count += getState(i + 1, j + 1);

        return count;
    }

    // 获取当前位置的值
    public int getState(int i, int j){

        // 超出边界即为dead
        if(i < 0 || i >= width || j < 0 || j >= height){
            return DEAD;
        }
        return this.board[i][j];
    }

    public static void main(String[] args) {
        Simulation simulation = new Simulation(8,5);

        simulation.setAlive(2,2);
        simulation.setAlive(3,2);
        simulation.setAlive(4,2);

        simulation.printBoard();

        simulation.step();
        simulation.printBoard();

        simulation.step();
        simulation.printBoard();
    }


}

